<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新建钱包</title>
    <link rel="stylesheet" type="text/css" href="../bootstrap3/css/bootstrap.css"/>
    <script src="../bootstrap3/jquery-1.9.1.min.js"></script>
</head>
<style>
    .Please{
        margin: 100px auto;
        width: 800px;
        height: 800px;
        background-color: rgb(109, 109, 109);
        text-align: center;
        line-height: 800px;
        font-size: 50px;
    }
</style>

<body>
    <div class="Please" data-toggle="modal" data-target="#NewWallet">
        <span>请先新建钱包</span>
    </div>
    <div class="modal fade" id="NewWallet" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <h4 class="modal-title" id="myModalLabel">新建钱包</h4>
            </div>
            <div class="modal-body">
                <div class="input-group">      
                  <div class="input-group-addon">电话号码</div>          
                  <input type="text" class="form-control" placeholder="电话号码" aria-describedby="basic-addon1" id="phone">
                </div>
                <br>
                <div class="input-group">      
                    <div class="input-group-addon">身份证号码</div>          
                    <input type="text" class="form-control" placeholder="身份证号码" aria-describedby="basic-addon1" id="identity">
                </div>
                <br>
                <div class="input-group">      
                  <div class="input-group-addon">支付密码</div>          
                  <input type="password" class="form-control" placeholder="支付密码" aria-describedby="basic-addon1" id="payment">
                </div>
                <br>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
              <button type="button" class="btn btn-primary" id="InsertNewWallet">确定</button>
            </div>
          </div>
        </div>
      </div>
<div id="userid"  th:text="${userid}" th:value="${userid}"></div>

</body>

<script src="../bootstrap3/js/bootstrap.min.js"></script>
<script>
    // userid=2// $(session.userid)//这里暂时写3
    userid=document.getElementById("userid").innerHTML
    //从session中取id
    sessionStorage.setItem("id",userid);
    $(document).ready(function (){
        $.ajax({
            url:'/wallet/getIsWallet/'+userid,
            type: 'get',
            success: function (data){
                if(data===1)
                {
                    window.location = "/wallet"
                }
                else {
                    $("#InsertNewWallet").click(function() {
                        let Wallet={
                            userid: Number(sessionStorage.getItem("id")),
                            identity: $("#identity").val(),
                            phone: $("#phone").val(),
                            payment: $("#payment").val(),
                            balance: 0
                        }
                        Wallet=JSON.stringify(Wallet)//转成json，因为开始是字符串
                        $.ajax({
                            url: '/wallet/Insert',
                            type: 'post',
                            data: Wallet,
                            contentType: "application/json",//设置数据类型，不然传不过去
                            success:function (data) {
                                alert(data)
                                window.location = "Wallet.html"
                            }
                        })
                    })
                }
            }
        })
    })


</script>
</html>